package com.letwal.cmcvnd.launch

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.login.ReqLogin
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.databinding.ActivityLoginInputBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.aviewmodel.LoginViewModel
import com.letwal.cmcvnd.utils.UserStateUtil

/** 登录-输入手机号页 */
@SuppressLint("CustomSplashScreen")
class LoginInputMobileActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityLoginInputBinding

    //登录viewModel
    private val mLoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoginViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.login_mobile.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityLoginInputBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //监听live data 返回
            onHandleLiveDataCallback()

            //点击确认
            mActivityBinding.submitLoginInputMobile.setOnClickListener {
                //直接登录
                checkAccount()
            }

            //手动注销
            UserStateUtil.logout()

            //光标聚集
            mActivityBinding.inputLoginUserName.toBeFocusPoint()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        //检查设备回调
        mLoginViewModel.onDetectUserInfoLiveData.observe(this) {
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    //组装传递参数
                    val email =
                        mActivityBinding.inputLoginUserName.getContent()
                    val password =
                        mActivityBinding.inputLoginPassword.getContent()
                    val params = HashMap<String, String>()
                    params["email"] = email
                    params["password"] = password
                    params["zjc"] = result.zjc ?: ""

                    //去登录，分两种：1输助词；2直接登录
                    if (result.isToLogin()) {
                        if (result.isNeedSetZjc()) {
                            //去登录，需要输入助记词
                            startToCreateWalletNewActivity(params)

                        } else if (result.isNeedInputZjc()) {
                            //去登录，需要输入助记词
                            startToInputWalletNewActivity(params)

                        } else if (result.isLoginDirectly()) {
                            //直接登录
                            doLogin()

                        } else {
                            //直接登录
                            doLogin()
                        }
                    } else {
                        //去注册，需要选择助记词
                        startToCreateWalletNewActivity(params)
                    }
                }
            }
        }

        //登录回调
        mLoginViewModel.onLoginLiveData.observe(this) {
            if (it.isSuccess) {
                startToHomeActivity()
            }
        }
    }

    /** 检查账号状态 */
    private fun checkAccount() {
        //直接登录
        val email =
            mActivityBinding.inputLoginUserName.getContent()
        val password =
            mActivityBinding.inputLoginPassword.getContent()
        if (email.isNotBlank() and password.isNotBlank()) {
            mLoginViewModel.detectUserInfo(
                ReqLogin(email = email, password = password, zjc = "")
            )

        } else {
            Toast.makeText(
                this,
                getString(R.string.please_complete_all_required_fields),
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    /** 去登录 */
    private fun doLogin() {
        //直接登录
        val email =
            mActivityBinding.inputLoginUserName.getContent()
        val password =
            mActivityBinding.inputLoginPassword.getContent()
        if (email.isNotBlank() and password.isNotBlank()) {
            mLoginViewModel.login(ReqLogin(email = email, password = password, zjc = ""))

        } else {
            Toast.makeText(
                this,
                getString(R.string.please_complete_all_required_fields),
                Toast.LENGTH_SHORT
            ).show()
        }
    }

}